package com.fig4ever.test.awt;

import java.awt.*;
import java.util.*;
import java.awt.event.*;

public class MyMouseAdapter {

	public static void main(String[] args) {
		new MyFrameMouse("drawing...");
	}

}

class MyFrameMouse extends Frame{
	ArrayList points = null;
	
	MyFrameMouse(String title) {
		
		super(title);
		points = new ArrayList();
		this.setLayout(null);
		this.setBounds(200, 200, 600, 400);
		this.setBackground(Color.white);
		this.setVisible(true);
		this.addMouseListener(new Mointor());
		this.addWindowListener(new MyWindowMonitor());
		this.addKeyListener(new MyKeyMonitor());
	}
	
	


	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		//super.print(g);
		Iterator i = points.iterator();
		while(i.hasNext()){
			Point p = (Point)i.next();
			g.setColor(Color.blue);
			g.fillRect(p.x,p.y,10,10);
			
		}
	}



	public void addPoint(Point p){
		points.add(p);
	}
	
}

class Mointor extends MouseAdapter{


	public void mousePressed(MouseEvent e) {
		MyFrameMouse f = (MyFrameMouse)e.getSource();
		f.addPoint(new Point(e.getX(),e.getY()));
		f.repaint();
	}
		
}

class MyWindowMonitor extends WindowAdapter{

	@Override
	public void windowClosing(WindowEvent e) {
		// TODO Auto-generated method stub
		super.windowClosing(e);
		MyFrameMouse f = (MyFrameMouse)e.getSource();
		f.setVisible(false);
		System.exit(0);
	}
	
}

class MyKeyMonitor extends KeyAdapter{

	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		super.keyPressed(e);
		
		if(e.getKeyCode() == KeyEvent.VK_UP){
			System.out.println("UP Key Pressed");
			MyFrameMouse f = (MyFrameMouse)e.getSource();
			f.setBackground(Color.CYAN);
			f.repaint();
		}
		if(e.getKeyCode() == KeyEvent.VK_DOWN){
			System.out.println("DOWN Key Pressed");
			MyFrameMouse f = (MyFrameMouse)e.getSource();
			f.setBackground(Color.darkGray);
			f.repaint();
		}
		if(e.getKeyCode() == KeyEvent.VK_LEFT){
			System.out.println("LEFT Key Pressed");
			MyFrameMouse f = (MyFrameMouse)e.getSource();
			f.setBackground(Color.magenta);
			f.repaint();
		}
		if(e.getKeyCode() == KeyEvent.VK_RIGHT){
			System.out.println("RIGHT Key Pressed");
			MyFrameMouse f = (MyFrameMouse)e.getSource();
			f.setBackground(Color.PINK);
			f.repaint();
		}
	}
	
}